package com.example.planetandzombiedemo;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Bundle;

import com.example.planetandzombiedemo.global.Config;
import com.example.planetandzombiedemo.global.DeviceHelper;
import com.example.planetandzombiedemo.view.GameView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        setContentView(new GameView(this));
    }

    private void init() {
        //获得设备尺寸
        Config.deviceWidth = DeviceHelper.getDeviceInfo(this)[0];
        Config.deviceHeight = DeviceHelper.getDeviceInfo(this)[1];
        //获得背景图片
        Config.gameBK = BitmapFactory.decodeResource(getResources(), R.drawable.bk);
        //获得缩放比，以背景图片为基准
        Config.scaleWidth = (float) Config.deviceWidth / Config.gameBK.getWidth();
        Config.scaleHeight = (float) Config.deviceHeight / Config.gameBK.getHeight();
        //使用缩放生成目标图片
        Config.gameBK = DeviceHelper.resizeBitmap(Config.gameBK);
        Config.seedBank = DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.seedbank));
        Config.seedBankLocationX = (Config.deviceWidth - Config.seedBank.getWidth()) / 2;
        //太阳花卡片
        Config.seedFlower = DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.seed_flower), Config.seedBank.getWidth() / 10,
                Config.seedBank.getHeight() * 8 / 10);
        //豌豆射手卡片
        Config.seedPea = DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.seed_pea), Config.seedBank.getWidth() / 10,
                Config.seedBank.getHeight() * 8 / 10);
        //太阳花
        Config.flowerFrames[0]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_1_01));
        Config.flowerFrames[1]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_1_02));
        Config.flowerFrames[2]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_1_03));
        Config.flowerFrames[3]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_1_04));
        Config.flowerFrames[4]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_1_05));
        Config.flowerFrames[5]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_1_06));
        Config.flowerFrames[6]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_1_07));
        Config.flowerFrames[7]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_1_08));
        //豌豆射手
        Config.peaFrames[0]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_2_01));
        Config.peaFrames[1]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_2_02));
        Config.peaFrames[2]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_2_03));
        Config.peaFrames[3]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_2_04));
        Config.peaFrames[4]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_2_05));
        Config.peaFrames[5]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_2_06));
        Config.peaFrames[6]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_2_07));
        Config.peaFrames[7]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.p_2_08));
        //游戏画面地图5行9列，同时定义每行的标识点
        //左边有2列，上边有一行，需要空出来
        for(int i=0;i<5;i++)
        {
            for(int j=0;j<9;j++)
            {
                Config.plantPoints.put(i*10+j,new Point((j+2)*Config.deviceWidth/11-Config.deviceWidth/11/2,
                        (i+1)*Config.deviceHeight/6));
            }
            Config.raceWayPoints[i]=(i+1)*Config.deviceHeight/6;
        }
        //阳光初始化
        Config.sun=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.sun));
        //豌豆子弹初始化
        Config.bullet=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.bullet));
        //僵尸初始化
        Config.zombieFrames[0]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.z_1_01));
        Config.zombieFrames[1]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.z_1_02));
        Config.zombieFrames[2]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.z_1_03));
        Config.zombieFrames[3]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.z_1_04));
        Config.zombieFrames[4]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.z_1_05));
        Config.zombieFrames[5]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.z_1_06));
        Config.zombieFrames[6]=DeviceHelper.resizeBitmap(BitmapFactory.decodeResource(getResources(),R.drawable.z_1_07));

        Config.heightDistance=Config.zombieFrames[0].getHeight()-Config.flowerFrames[0].getHeight();
    }
}
